package com.jindy.module.AssetReceive.service.impl;

import java.util.Date;
import java.util.List;

import com.jindy.common.core.domain.entity.SysUser;
import com.jindy.common.core.domain.model.LoginUser;
import com.jindy.common.utils.DateUtils;
import com.jindy.common.utils.SecurityUtils;
import com.jindy.module.AssetCheck.domain.AssetCheck;
import com.jindy.module.AssetCheck.mapper.AssetCheckMapper;
import com.jindy.module.AssetDetails.domain.AssetDetails;
import com.jindy.module.AssetDetails.mapper.AssetDetailsMapper;
import com.jindy.module.AssetRepair.domain.AssetRepair;
import com.jindy.module.AssetRepair.mapper.AssetRepairMapper;
import com.jindy.module.AssetRepair.service.IAssetRepairService;
import com.jindy.system.mapper.SysUserMapper;
import com.jindy.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jindy.module.AssetReceive.mapper.AssetReceiveMapper;
import com.jindy.module.AssetReceive.domain.AssetReceive;
import com.jindy.module.AssetReceive.service.IAssetReceiveService;

/**
 * 资产领用Service业务层处理
 *
 * @author jindy
 * @date 2023-04-20
 */
@Service
public class AssetReceiveServiceImpl implements IAssetReceiveService
{
    @Autowired
    private AssetReceiveMapper assetReceiveMapper;

    @Autowired
    private AssetDetailsMapper assetDetailsMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private AssetRepairMapper assetRepairMapper;

    @Autowired
    private AssetCheckMapper assetCheckMapper;


    /**
     * 查询资产领用
     *
     * @param id 资产领用主键
     * @return 资产领用
     */
    @Override
    public AssetReceive selectAssetReceiveById(Long id)
    {
        return assetReceiveMapper.selectAssetReceiveById(id);
    }

    /**
     * 查询资产领用列表
     *
     * @param assetReceive 资产领用
     * @return 资产领用
     */
    @Override
    public List<AssetReceive> selectAssetReceiveList(AssetReceive assetReceive)
    {
        return assetReceiveMapper.selectAssetReceiveList(assetReceive);
    }

    /**
     * 新增资产领用
     *
     * @param assetReceive 资产领用
     * @return 结果
     */
    @Override
    public int insertAssetReceive(AssetReceive assetReceive)
    {
        int flag=0;
        //获取领用物品的id
        Long assetId=assetReceive.getReceiveAssetId();
        //查询当前要添加的物品信息
        AssetDetails assetDetails=assetDetailsMapper.selectAssetDetailsById(assetId);

        //领用数量与可用数量校验
        if(assetReceive.getReceiveQuantity() <= assetDetails.getAssetAvailQuantity()) {
            //状态改为使用中
            assetReceive.setIsReturn("使用中");
            //查询相应信息【待确认】
            assetReceive.setReceiveClassName(assetDetails.getClassName());
            //查询用户信息
            LoginUser loginUser = SecurityUtils.getLoginUser();
            if (assetReceive.getReceiveUserId() != null) {
                SysUser sysUser = sysUserMapper.selectUserById(assetReceive.getReceiveUserId());
                assetReceive.setReceiveDeptId(sysUser.getDeptId());
                assetReceive.setReceiveDeptName(sysUser.getDept().getDeptName());
            }
            assetReceive.setCreateTime(DateUtils.getNowDate());

            //执行新增
            flag = assetReceiveMapper.insertAssetReceive(assetReceive);
            long receId=assetReceive.getId();

            if (flag == 1) {
                //修改资产明细表相应可用物品数量
                assetDetails.setAssetAvailQuantity(assetDetails.getAssetAvailQuantity() - assetReceive.getReceiveQuantity());
                assetDetails.setUpdateTime(DateUtils.getNowDate());
                assetDetailsMapper.updateAssetDetails(assetDetails);

                //资产盘点表新增资产领用
                AssetCheck assetCheck=new AssetCheck();
                assetCheck.setAssetId(assetReceive.getReceiveAssetId());
                assetCheck.setAssetName(assetReceive.getReceiveAssetName());
                assetCheck.setOperateType("资产领用");
                assetCheck.setOperateId(receId);
                assetCheck.setOperatePersonId(assetReceive.getReceiveUserId());
                assetCheck.setOperateTime(DateUtils.getNowDate());
                assetCheckMapper.insertAssetCheck(assetCheck);
            }
        }
        return flag;
    }

    /**
     * 修改资产领用
     *
     * @param assetReceive 资产领用
     * @return 结果
     */
    @Override
    public int updateAssetReceive(AssetReceive assetReceive)
    {
        assetReceive.setUpdateTime(DateUtils.getNowDate());
        return assetReceiveMapper.updateAssetReceive(assetReceive);
    }

    /**
     * 批量删除资产领用
     *
     * @param ids 需要删除的资产领用主键
     * @return 结果
     */
    @Override
    public int deleteAssetReceiveByIds(Long[] ids)
    {
        return assetReceiveMapper.deleteAssetReceiveByIds(ids);
    }

    /**
     * 删除资产领用信息
     *
     * @param id 资产领用主键
     * @return 结果
     */
    @Override
    public int deleteAssetReceiveById(Long id)
    {
        return assetReceiveMapper.deleteAssetReceiveById(id);
    }

    /**
     * 修改归还状态
     *
     * @param assetReceive 资产领用
     * @return 结果
     */
    @Override
    public int updateIsReturn(AssetReceive assetReceive)
    {
        int flag=0;
        AssetReceive assetReceive1=assetReceiveMapper.selectAssetReceiveById(assetReceive.getId());
        //状态为使用中的机器可归还
        if (assetReceive1.getIsReturn().equals("使用中")){
            assetReceive.setIsReturn("已归还");
            assetReceive.setUpdateTime(DateUtils.getNowDate());
            flag=assetReceiveMapper.updateAssetReceive(assetReceive);
            long receId=assetReceive.getId();

            if(flag==1) {
                //修改资产明细表中可用数量
                AssetDetails assetDetails = assetDetailsMapper.selectAssetDetailsById(assetReceive.getReceiveAssetId());
                assetDetails.setAssetAvailQuantity(assetDetails.getAssetAvailQuantity() + assetReceive.getReceiveQuantity());
                assetDetails.setUpdateTime(DateUtils.getNowDate());
                assetDetailsMapper.updateAssetDetails(assetDetails);

                //资产盘点表新增资产归还
                AssetCheck assetCheck=new AssetCheck();
                assetCheck.setAssetId(assetReceive.getReceiveAssetId());
                assetCheck.setAssetName(assetReceive.getReceiveAssetName());
                assetCheck.setOperateType("资产归还");
                assetCheck.setOperateId(receId);
                assetCheck.setOperateTime(DateUtils.getNowDate());
                assetCheckMapper.insertAssetCheck(assetCheck);
            }
        }
        return flag;
    }

    /**
     * 资产报修
     *
     * @param assetReceive 资产领用对象
     * @return 结果
     */
    @Override
    public int updateIsRepair(AssetReceive assetReceive){
        int flag=0;
        if (assetReceive.getId() != null && assetReceive.getIsReturn().equals("使用中")){
            //资产报修新增
            AssetRepair assetRepair=new AssetRepair();
            assetRepair.setAssetId(assetReceive.getReceiveAssetId());
            assetRepair.setAssetName(assetReceive.getReceiveAssetName());
            assetRepair.setClassName(assetReceive.getReceiveClassName());
            assetRepair.setAssetBrand(assetReceive.getReceiveAssetBrand());
            assetRepair.setAssetModel(assetReceive.getReceiveAssetModel());
            assetRepair.setUserId(assetReceive.getReceiveUserId());
            assetRepair.setUserName(assetReceive.getReceiveUserName());
            assetRepair.setUserDeptId(assetReceive.getReceiveDeptId());
            assetRepair.setUserDeptName(assetReceive.getReceiveDeptName());
            assetRepair.setAssetQuantity(assetReceive.getReceiveQuantity());
            assetRepair.setAssetReceiveId(assetReceive.getId());
            assetRepair.setRepairStatus("维修中");
            assetRepair.setCreateTime(DateUtils.getNowDate());
            flag=assetRepairMapper.insertAssetRepair(assetRepair);
            long receId=assetReceive.getId();

            if(flag == 1){
                //资产领用状态修改
                if(assetReceive.getIsReturn().equals("使用中")){
                    assetReceive.setIsReturn("维修中");
                    assetReceive.setUpdateTime(DateUtils.getNowDate());
                    assetReceiveMapper.updateAssetReceive(assetReceive);

                    //资产盘点表新增资产报修
                    AssetCheck assetCheck=new AssetCheck();
                    assetCheck.setAssetId(assetReceive.getReceiveAssetId());
                    assetCheck.setAssetName(assetReceive.getReceiveAssetName());
                    assetCheck.setOperateType("资产报修");
                    assetCheck.setOperateId(receId);
                    assetCheck.setOperateTime(DateUtils.getNowDate());
                    assetCheckMapper.insertAssetCheck(assetCheck);
                }
            }
        }
        return flag;
    }
}
